# @Time     : 2019/4/15 14:45
# -*- coding : utf-8 -*-
# @Author   : GKL

import re
import base64
from io import BytesIO
from lxml import etree
import requests
from fontTools.ttLib import TTFont


class Spider(object):
    def __init__(self):
        self.url = "https://fz.58.com/searchjob/?key=%E4%BA%92%E8%81%94%E7%BD%91"
        self.headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 \
            (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
        }

    def get_base64_data(self):
        resp = requests.get(self.url, headers=self.headers).text
        base64_str = re.findall(r"base64,(.*?)\)", resp)[0]  # base64 字符串
        return base64_str, resp

    def make_xml(self, base64_str):
        data = base64.decodebytes(base64_str.encode())
        font = TTFont(BytesIO(data))
        font.saveXML("result.xml")

    def parse_xml(self, filename):
        tree = etree.parse(filename)
        page = tree.getroot()
        # 编码和name属性之间的关系
        unicode_items = {}
        for i in page.xpath("//map"):
            unicode_items[i.get("code")] = i.get("name")
        # name属性和轨迹之间的关系
        items = {}
        for i in page.xpath("//TTGlyph"):
            value = re.findall(r'(xMin=".*?" yMin=".*?" xMax=".*?" yMax=".*?")', etree.tostring(i).decode(), re.S)
            if value:
                items[i.get("name")] = value[0]




    def run(self):
        base64_str, resp = self.get_base64_data()
        self.make_xml(base64_str)
        self.parse_xml("result.xml")



if __name__ == '__main__':
    s = Spider()
    s.run()
